/******************************************************************************
** 
** Letters frequencies histogramm.
** 
** File name:	letters_frequencies_histogramm.c
** Compilation:	cc letters_frequencies_histogramm.c -o letters_frequencies_histogramm
** Execution:	./letters_frequencies_histogramm
** 
** Description:
** Reads the input and prints the horizontal histogramm of English alphabet 
** letters frequences. 'a'-'z' and 'A'-'Z' are treated as the same.
** 
******************************************************************************/
#include <stdio.h>

#define H_MAX_LENGTH 70     // histogramm's string maximum length
#define H_ELEMENT "#"       // element which forms a histogramm line
#define NUM_LETTERS_IN_ALPHABET 26      // number of letters in English alphabet
#define LOWERCASE_OFFSET 97
#define UPPERCASE_OFFSET 65

int c, i, j;
int letters[NUM_LETTERS_IN_ALPHABET];
float highest_frequency;

main()
{
    for (i=0; i<=NUM_LETTERS_IN_ALPHABET-1; ++i) {
      letters[i]=0;
    }
    
    // reading the input, counting every letter appearance in letters[]
    while ((c=getchar()) != EOF) {
      if (c >= 'a' && c <= 'z') {
        ++letters[c-LOWERCASE_OFFSET];
      } else if (c >= 'A' && c <= 'Z') {
        ++letters[c-UPPERCASE_OFFSET];
      }
    }
    
    // defining the highest frequency
    highest_frequency = 0;
    for (i=0; i<=NUM_LETTERS_IN_ALPHABET-1; ++i) {
      if (highest_frequency < letters[i]) {
        highest_frequency = letters[i];
      }
    }
    
    // printing the histogramm
    printf("\n");
    for (i=0; i<=NUM_LETTERS_IN_ALPHABET-1; ++i) {
      printf("%c %4d | ", i+LOWERCASE_OFFSET, letters[i]);
      for (j=1; j<=letters[i]/highest_frequency*H_MAX_LENGTH; ++j) {
        printf("%s", H_ELEMENT);
      }
      printf("\n");
    }
}

